package com.tengju.user.infrastructure.listener;

import com.tengju.user.domain.model.alert.AlertEvent;
import com.tengju.user.domain.model.alert.AlertService;
import com.tengju.user.domain.model.user.StarChangeEvent;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.service.UserDocService;
import com.tengju.user.domain.shared.DateUtil;
import com.tengju.user.domain.shared.JsonUtil;
import com.tengju.user.domain.shared.SpringEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;


/**
 * ec，sec, eec星级改变监听器
 *
 * @author 大木
 * @date 2021/11/19
 */
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SecEecStarChangeListener implements ApplicationListener<SpringEvent>, DomainEventListener {

    private final AlertService alertService;
    private final UserDocService userDocService;

    @Value("${alert.seceec.star.change.notice.robot.url}")
    private String alertUrl;

    @Override
    public void onApplicationEvent(SpringEvent springEvent) {
        StarChangeEvent starChangeEvent = checkDomainEvent(springEvent, StarChangeEvent.class);
        if (starChangeEvent == null) {
            return;
        }
        log.info("收到星级变更消息：{}", JsonUtil.toJSONString(starChangeEvent));
        processWithStarFeature(starChangeEvent);
    }

    private void processWithStarFeature(StarChangeEvent starChangeEvent) {
        String userNickName = "-";
        String userRealName = "-";
        if (starChangeEvent.getBeforeStarLevel() >= StarLevelEnum.SEC.getCode()
                || starChangeEvent.getAfterStarLevel() >= StarLevelEnum.SEC.getCode()) {
            List<UserInfo> userInfos = userDocService.searchUserByIdCodes(List.of(new UserIdCode(starChangeEvent.getIdCode())));
            if (!CollectionUtils.isEmpty(userInfos)){
                UserInfo userInfo = userInfos.get(0);
                userNickName = userInfo.getNickName();
                userRealName = userInfo.getName();
            }
            AlertEvent alertEvent = new AlertEvent("星级变更");
            alertEvent.addItem("用户Id ", starChangeEvent.getIdCode());
            alertEvent.addItem("用户昵称 ", userNickName);
            alertEvent.addItem("用户姓名 ", userRealName);
            alertEvent.addItem("变更前等级 ", StarLevelEnum.parseByCode(starChangeEvent.getBeforeStarLevel()).getShortName());
            alertEvent.addItem("变更后等级 ", StarLevelEnum.parseByCode(starChangeEvent.getAfterStarLevel()).getShortName());
            alertEvent.addItem("变更操作人 ", StringUtils.replaceChars(starChangeEvent.getUpdateBy(),"admin","系统"));
            alertEvent.addItem("变更时间 ", DateUtil.formartLocalDateTime(DateUtil.DATE_TIME_FORMAT, LocalDateTime.now()));
            alertService.businessNotice(alertEvent);
            if (StringUtils.isNotBlank(alertUrl)){
                alertService.businessNotice(alertEvent, alertUrl);
            }
        }
    }
}
